package io.renren.modules.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.order.dao.OrderContractDao;
import io.renren.modules.order.dto.OrderContractDTO;
import io.renren.modules.order.entity.OrderContractEntity;
import io.renren.modules.order.service.OrderContractService;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 合同表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-12-07
 */
@Slf4j
@Service
public class OrderContractServiceImpl extends CrudServiceImpl<OrderContractDao, OrderContractEntity, OrderContractDTO> implements OrderContractService {

    @Override
    public QueryWrapper<OrderContractEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<OrderContractEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public List<OrderContractDTO> getContractListByOrderIdList(List<Long> orderIdList) {
        log.info("根据订单ID查询合同表记录");
        if (CollectionUtils.isEmpty(orderIdList)) {
            return Collections.EMPTY_LIST;
        }
        QueryWrapper<OrderContractEntity> wrapper = new QueryWrapper<>();
        wrapper.in("order_id", orderIdList);
        List<OrderContractEntity> entityList = this.baseDao.selectList(wrapper);
        return ConvertUtils.sourceToTarget(entityList, OrderContractDTO.class);
    }

    @Override
    public OrderContractDTO getByOrderId(Long orderId) {
        log.info("根据订单ID获取合同记录");
        QueryWrapper<OrderContractEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderId);
        List<OrderContractEntity> entityList = this.baseDao.selectList(wrapper);
        List<OrderContractDTO> contractDtoList = ConvertUtils.sourceToTarget(entityList, OrderContractDTO.class);
        if (CollectionUtils.isEmpty(contractDtoList)) {
            return null;
        }
        return contractDtoList.get(0);
    }

    @Override
    public void deleteByOrderId(Long orderId) {
        log.info("根据订单ID删除合同记录");
        QueryWrapper<OrderContractEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderId);
        this.baseDao.delete(wrapper);
    }

    @Override
    public List<OrderContractDTO> getAllContractList(Map<String, Object> params) {
        String startDate = (String) params.get("startDate");
        String endDate = (String) params.get("endDate");
        QueryWrapper<OrderContractEntity> wrapper = new QueryWrapper<>();
        wrapper.select("id", "order_id");
        if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            wrapper.between("create_date", startDate, endDate);
        }
        List<OrderContractEntity> entityList = this.baseDao.selectList(wrapper);
        return ConvertUtils.sourceToTarget(entityList, OrderContractDTO.class);
    }

}